<?php

namespace sqlhelp;


class sqlhelp
{
    private $dbHost     = "localhost";
    private $dbUsername = "djb97013266";
    private $dbPassword = "djb97013266";
    private $dbName     = "djb97013266";

    public function __construct(){
        if(!isset($this->db)){
            // Connect to the database
            $conn = new \mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
                mysqli_query($conn, "set names utf8"); //utf8 设为对应的编码
            }
        }
    }

    public function get_row($sql){
        $result = $this->db->query($sql);
        if ($result->num_rows > 0) {
            // output value? check it later before submit to blackbroad
            return $result->fetch_assoc();
        }
        return null;
    }

    public function get_rows($sql){
        $result = $this->db->query($sql);
        $rows = [];
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }
        }
        return $rows;
    }

    public function query($sql){
        return $this->db->query($sql);
    }
}